package com.wms.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wms.dto.ConfirmPickDto;
import com.wms.dto.OutboundOrderPickDto;
import com.wms.mapper.OutboundOrderDetailsMapper;
import com.wms.pojo.OutboundOrder;
import com.wms.pojo.OutboundOrderDetails;
import com.wms.pojo.OutboundOrderPackage;
import com.wms.pojo.PageResult;
import com.wms.service.OutboundOrderDetailsService;
import com.wms.service.OutboundOrderPackageService;
import com.wms.service.OutboundOrderService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class OutboundOrderDetailsServiceImpl extends ServiceImpl<OutboundOrderDetailsMapper, OutboundOrderDetails> implements OutboundOrderDetailsService {
    private final OutboundOrderService orderService;
    private final OutboundOrderPackageService packageService;

    @Override
    public PageResult queryPagePick(Integer page, Integer pageSize, Integer status, String invoiceCode, String customerName) {
        PageHelper.startPage(page,pageSize);
        List<OutboundOrderPickDto> list = getBaseMapper().queryPagePick(status,invoiceCode,customerName);
        Page<OutboundOrderPickDto> pickDtoPage = (Page<OutboundOrderPickDto>) list;
        return new PageResult(pickDtoPage.getTotal(), pickDtoPage.getResult());
    }

    //确定拣货
    @Override
    @Transactional
    public void updatePickCount(ConfirmPickDto dto) {
        OutboundOrderDetails orderDetails = getById(dto.getId());
        OutboundOrderDetails details = OutboundOrderDetails.builder().id(dto.getId())
                .updateTime(LocalDateTime.now())
                .pickEmpId(dto.getPickEmpId())
                .pickStatus(2)
                .pickTime(dto.getPickTime()).build();
        updateById(details);
        //未打包添加数据
        // 生成8位数字随机数
        int eightDigitNumber = (int)(Math.random() * 90000000 + 10000000);
        String packageCode = "pack"+eightDigitNumber;
        OutboundOrderPackage orderPackage = OutboundOrderPackage.builder()
                .packageCode(packageCode)
                .outboundOrderDetailId(orderDetails.getId())
                .status(1)
                .packageCount(orderDetails.getOutCount())
                .skuId(orderDetails.getSkuId())
                .updateTime(LocalDateTime.now())
                .createTime(LocalDateTime.now())
                .build();
        packageService.save(orderPackage);

        List<OutboundOrderDetails> list = getBaseMapper().selectList(Wrappers.<OutboundOrderDetails>lambdaQuery()
                .eq(OutboundOrderDetails::getOutboundOrderId, orderDetails.getOutboundOrderId()));
        // 判断是否所有明细都已完成拣货（pickStatus都为2）
        boolean allPicked = list.stream()
                .allMatch(item -> Integer.valueOf(2).equals(item.getPickStatus()));
        OutboundOrder outboundOrder = OutboundOrder.builder().id(orderDetails.getOutboundOrderId())
                .updateTime(LocalDateTime.now()).build();
        if (allPicked){
            outboundOrder.setStatus(4);
            orderService.updateById(outboundOrder);
        }else {
            outboundOrder.setStatus(3);
            orderService.updateById(outboundOrder);
        }
    }
}
    